import { 
    userList,
    userAdd,
    userUpdate,
    userDelete,
    userAllDelete,
    userRoles,
    userAddRoles,
    userRemoveRoles,
    getUserLoginInfo,
    setUserLoginInfo
 } from "@/api/user";

export const userStore = {
    namespaced: true,
    state: {
        //  el-table-column每行数据对应的用户的id
        rowUserId: 0,
        //  el-table-column每行数据对应的用户的用户名
        rowUserName: ""
    },
    mutations: {
        setRowUserId(state, value){
            state.rowUserId = value;
        },
        setRowUserName(state, value){
            state.rowUserName = value;
        }
    },
    actions: {
        //  获取登录用户登录信息
        async getUserLoginInfo(context, data){
            try {
                const { id } = data;
                const result = await getUserLoginInfo(id);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "获取数据失败");
                }
            } catch (error) {
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },

        //  设置登录用户登录信息
        async setUserLoginInfo(context, data){
            try {
                const result = await setUserLoginInfo(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "设置数据失败");
                }
            } catch (error) {
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },


        //  获取所有用户信息
        async userList(context, data){
            try {
                const { curPage, pageSize, name} = data;
                const list = await userList(pageSize,curPage,name);
                if(list.code === 200){
                    list.data.forEach(userObj => {
                        userObj.role_descriptions = userObj.role_descriptions.join("、");
                        userObj.role_names = userObj.role_names.join("、");
                    });
                    return list;
                }else{
                    return Promise.reject(new Error(list.msg) || "获取数据失败");
                }
            } catch (error) {
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },

        //  添加用户信息
        async userAdd(context, data){
            try{
                const result = await userAdd(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "添加数据失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },

        //  更新用户信息
        async userUpdate(context, data){
            try{
                const result = await userUpdate(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "更新数据失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },

        //  删除用户信息
        async userDelete(context, data){
            try{
                const {id} = data;
                const result = await userDelete(id);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "删除数据失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },

        //  批量删除用户信息
        async userAllDelete(context, data){
            try{
                const result = await userAllDelete(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "批量删除数据失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },

        //  根据用户id获取用户角色信息
        async userRoles(context, data){
            const { user_id } = data;
            try{
                const result = await userRoles(user_id);
                if(result.code === 200){
                    return result.data;
                }else{
                    return Promise.reject(new Error(result.msg) || "获取数据失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        
        //  给用户分配角色
        async userAssignRoles(context, data){
            try {
                const result = await userAddRoles(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "角色分配失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        },
        
        //  移除用户角色
        async userRemoveRoles(context, data){
            try {
                const result = await userRemoveRoles(data);
                if(result.code === 200){
                    return result;
                }else{
                    return Promise.reject(new Error(result.msg) || "角色移除失败");
                }
            }catch(error){
                return Promise.reject(new Error(error.response?.data?.msg || "网络或服务器异常"));
            }
        }
    },
    getters: {
        
    }
}